//
//  UIButton+Extension.swift
//  QingLiuSwift
//
//  Created by xyanl on 2023/7/31.
//

import UIKit

extension UIButton {
    /// 设置按钮的背景色
    /// - Parameters:
    ///   - color: 背景色
    ///   - state: 状态
    func setBackgroundColor(_ color: UIColor, for state: UIControl.State) {
        self.setBackgroundImage(color.toImage(), for: state)
    }
}

//extension UIButton {
//    
//    private var hitTestEdgeInsets: UIEdgeInsets? {
//        get {
//            return objc_getAssociatedObject(self, &AssociatedKeys.hitTestEdgeInsets) as? UIEdgeInsets
//        }
//        set {
//            objc_setAssociatedObject(self, &AssociatedKeys.hitTestEdgeInsets, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
//        }
//    }
//
//    private struct AssociatedKeys {
//        static var hitTestEdgeInsets = "hitTestEdgeInsets"
//    }
//    
//    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
//        if let hitTestEdgeInsets = self.hitTestEdgeInsets {
//            let hitArea = bounds.inset(by: hitTestEdgeInsets)
//            return hitArea.contains(point)
//        }
//        return super.point(inside: point, with: event)
//    }
//    
//    public func setHitTestEdgeInsets(_ edgeInsets: UIEdgeInsets) {
//        self.hitTestEdgeInsets = edgeInsets
//    }
//}


extension UIButton {
    
    private static var Expand_Key: Void?
    private static var Touch_Key: Void?
    
    /// 设置四周扩大范围
     var touchExpand: CGFloat {
        get {
            (objc_getAssociatedObject(self, &Self.Expand_Key) as? CGFloat) ?? 0.0
        }
        set {
            objc_setAssociatedObject(self, &Self.Expand_Key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
            hitTestEdgeInsets = UIEdgeInsets(top: newValue, left: newValue, bottom: newValue, right: newValue)
        }
    }
    
    /// 设置扩展按钮的点击区域
    public func setHitTestEdgeInsets(_ edgeInsets: UIEdgeInsets) {
        hitTestEdgeInsets = edgeInsets
    }
    
    /// 扩展按钮的点击区域
    private var hitTestEdgeInsets: UIEdgeInsets {
        get {
            (objc_getAssociatedObject(self, &Self.Touch_Key) as? UIEdgeInsets) ?? .zero
        }
        set {
            objc_setAssociatedObject(self, &Self.Touch_Key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
    }
    
    private func expandRect() -> CGRect {
        let touchEdgeInsets: UIEdgeInsets? = hitTestEdgeInsets
        if (touchEdgeInsets != .zero) {
            return CGRect(
                x: bounds.origin.x - touchEdgeInsets!.left,
                y: bounds.origin.y - touchEdgeInsets!.top,
                width: bounds.size.width + touchEdgeInsets!.left + touchEdgeInsets!.right,
                height: bounds.size.height + touchEdgeInsets!.top + touchEdgeInsets!.bottom
            )
        } else{
            return bounds
        }
    }
    
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let buttonRect = expandRect()
        if (buttonRect.equalTo(bounds)) {
            return super.point(inside: point, with: event)
        } else{
            return buttonRect.contains(point)
        }
    }
}

extension UIButton {
    
    /// 按钮交互状态, 带有蒙板
    /// - Parameter enabled: true: 可以点击交互，false: 不可交互,会有一层蒙板
    func setUserInteractionEnabled(_ enabled: Bool) {
        self.isUserInteractionEnabled = enabled
        self.subviews.forEach { view in
            if view.isKind(of: UIVisualEffectView.self) {
                view.removeFromSuperview()
            }
        }
        
        if !enabled {
            // 不能交互，加蒙版
            let blurEffect = UIBlurEffect(style: .regular)
            let blurView = UIVisualEffectView(effect: blurEffect)
            blurView.alpha = 0.3
            blurView.frame = self.bounds
            self.addSubview(blurView)
        }   
    }
}

